import {
  reqgetcate,
  reqgetbanner,
  reqgetnew,
  reqgetindexgoods,
  reqgetgroupon,
  reqgetseckill,
} from "../../http/api";
//state
const initState = {
  //初始化分类
  cate: [],
  banner: [],
  getnew: [],
  goods: [],
  n: 0,
  wanrentuan: [],
  seckill: [],
//   miaosha:{}
};

//action type
const TYPES = {
  HOME_CHANGE_CATE: "HOME_CHANGE_CATE",
  HOME_CHANGE_BANNER: "HOME_CHANGE_BANNER",
  HOME_CHANGE_GETNEW: "HOME_CHANGE_GETNEW",
  HOME_CHANGE_GOODS: "HOME_CHANGE_GOODS",
  HOME_CHANGE_N: "HOME_CHANGE_N",
  HOME_CHANGE_WANRENTUAN: "HOME_CHANGE_WANRENTUAN",
  HOME_CHANGE_SECKILL: "HOME_CHANGE_SECKILL",
//   HOME_CHANGE_MIAOSHA:"HOME_CHANGE_MIAOSHA"
};

//reducer
const reducer = (state = initState, action) => {
  switch (action.type) {
    case TYPES.HOME_CHANGE_CATE:
      return {
        ...state,
        cate: action.cate,
      };
    case TYPES.HOME_CHANGE_BANNER:
      return {
        ...state,
        banner: action.banner,
      };
    case TYPES.HOME_CHANGE_GETNEW:
      return {
        ...state,
        getnew: action.getnew,
      };
    case TYPES.HOME_CHANGE_GOODS:
      return {
        ...state,
        goods: action.goods,
      };
    case TYPES.HOME_CHANGE_N:
      return {
        ...state,
        n: action.n,
      };
    case TYPES.HOME_CHANGE_WANRENTUAN:
      return {
        ...state,
        wanrentuan: action.wanrentuan,
      };
    case TYPES.HOME_CHANGE_SECKILL:
      return {
        ...state,
        seckill: action.seckill,
      };
    //   case TYPES.HOME_CHANGE_MIAOSHA:
    //     return{
    //         ...state,
    //         miaosha:action.miaosha
    //     }
    default:
      return state;
  }
};

//action creator
export const actions = {
  changeCate: (cate) => ({ type: TYPES.HOME_CHANGE_CATE, cate }),
  reqCate: () => (dispatch, getState) => {
    //将redux状态层作为缓存层
    let cates=getCate(getState());
    if(cates.length>0){
          return
    }
    reqgetcate().then((res) => {
      //修改cate
      dispatch(actions.changeCate(res.data.list));
    });
  },
  changeBanner: (banner) => ({ type: TYPES.HOME_CHANGE_BANNER, banner }),
  reqBanner: () => (dispatch, getState) => {
      //将redux状态层作为缓存层
    let banners=getBanner(getState());
    if(banners.length>0){
          return
    }
    reqgetbanner().then((res) => {
      dispatch(actions.changeBanner(res.data.list));
    });
  },
  changeGetnew: (getnew) => ({ type: TYPES.HOME_CHANGE_GETNEW, getnew }),
  reqGetnew: () => (dispatch, getState) => {
    let news=getNew(getState());
    if(news.length>0){
          return
    }
    reqgetnew().then((res) => {
      dispatch(actions.changeGetnew(res.data.list));
    });
  },

  changeGoods: (goods) => ({ type: TYPES.HOME_CHANGE_GOODS, goods }),
  reqgetgoods: () => (dispatch, getState) => {
    let goods=getGoods(getState());
    if(goods.length>0){
          return
    }
    reqgetindexgoods().then((res) => {
      dispatch(actions.changeGoods(res.data.list));
    });
  },
  changeN: (n) => ({ type: TYPES.HOME_CHANGE_N, n }),

  changeWANRENTUAN: (wanrentuan) => ({
    type: TYPES.HOME_CHANGE_WANRENTUAN,
    wanrentuan,
  }),
  reqwanrentuan: () => (dispatch, getState) => {
    let wanren=getWanren(getState());
    if(wanren.length>0){
          return
    }
    reqgetgroupon().then((res) => {
      dispatch(actions.changeWANRENTUAN(res.data.list));
    });
  },
  changeSeckill: (seckill) => ({ type: TYPES.HOME_CHANGE_SECKILL, seckill }),
  reqSeckill: () => (dispatch, getState) => {
    let seckil=getSkill(getState());
    if(seckil.length>0){
          return
    }
    reqgetseckill().then((res) => {
      dispatch(actions.changeSeckill(res.data.list));
    });
  },
//   changeMiaosha:(miaosha)=>({type:TYPES.HOME_CHANGE_MIAOSHA,miaosha}),
//   computedTime() {
//     this.time = 1658899658737;
//     this.timer = setInterval(() => {
//       let d = new Date().getTime();
//       let t = this.time - d; //差的毫秒数
//       if (t > 0) {
//         let day = (Math.floor(t / (24 * 60 * 60 * 1000)) + "").padStart(2, "0"); //天
//         t = t % (24 * 60 * 60 * 1000); //剩余计算小时的毫秒数
//         let hour = (Math.floor(t / (60 * 60 * 1000)) + "").padStart(2, "0"); //时
//         t = t % (60 * 60 * 1000);
//         let minute = (Math.floor(t / (60 * 1000)) + "").padStart(2, "0"); //分
//         t = t % (60 * 1000);
//         let second = (Math.floor(t / 1000) + "").padStart(2, "0"); //秒
//         let mSecond = ((t % 1000) + "").padStart(3, "0"); //毫秒

//        return{
//         timeString: { day, hour, minute, second, mSecond },
//        }
         
      
//       } else {
//         return{
//             timeString: "秒杀活动已经结束",
//         }
         
       
//       }
//     }, 1);
//     console.log(timeString);
//   }
};

//selector导出数据给组件
export const getCate = (state) => state.home.cate;
export const getBanner = (state) => state.home.banner;
export const getNew = (state) => state.home.getnew;
export const getGoods = (state) => state.home.goods;
export const getN = (state) => state.home.n;
export const getWanren = (state) => state.home.wanrentuan;
export const getSkill = (state) => state.home.seckill;
//导出reducer
export default reducer;
